package com.ruoyi.stuinfo.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.ruoyi.stuinfo.domain.AbnormalStudentDTO;
import com.ruoyi.stuinfo.domain.Attendancerecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * 考勤记录Mapper接口
 *
 * @author zc
 * @date 2025-05-14
 */
public interface AttendancerecordMapper
{
    /**
     * 查询考勤记录
     *
     * @param recordId 考勤记录主键
     * @return 考勤记录
     */
    public Attendancerecord selectAttendancerecordByRecordId(Long recordId);

    /**
     * 查询考勤记录列表
     *
     * @param attendancerecord 考勤记录
     * @return 考勤记录集合
     */
    public List<Attendancerecord> selectAttendancerecordList(Attendancerecord attendancerecord);

    /**
     * 新增考勤记录
     *
     * @param attendancerecord 考勤记录
     * @return 结果
     */
    public int insertAttendancerecord(Attendancerecord attendancerecord);

    /**
     * 修改考勤记录
     *
     * @param attendancerecord 考勤记录
     * @return 结果
     */
    public int updateAttendancerecord(Attendancerecord attendancerecord);

    /**
     * 删除考勤记录
     *
     * @param recordId 考勤记录主键
     * @return 结果
     */
    public int deleteAttendancerecordByRecordId(Long recordId);

    /**
     * 批量删除考勤记录
     *
     * @param recordIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteAttendancerecordByRecordIds(Long[] recordIds);

    public List<Attendancerecord> selectAttendancerecordList_handteacher(String teacherId);

    public List<Attendancerecord> selectAttendancerecordList_Xiteacher(String teacherId);

    @Select("SELECT " +
            "COUNT(CASE WHEN check_type = '正常' THEN 1 ELSE NULL END) AS normal, " +
            "COUNT(CASE WHEN check_type = '晚归' THEN 1 ELSE NULL END) AS late, " +
            "COUNT(CASE WHEN check_type = '不归' THEN 1 ELSE NULL END) AS absent " +
            "FROM attendancerecord")
    Map<String, Long> countAttendanceStatus();


    @Select("<script>" +
            "SELECT " +
            "ar.record_id, " +
            "ar.student_id, " +
            "si.name AS student_name, " +
            "c.cname AS class_name, " +
            "ar.room_id, " +
            "ar.check_type, " +
            "ar.check_date, " +
            "ar.check_time " +
            "FROM attendancerecord ar " +
            "JOIN stu_info si ON ar.student_id = si.student_id " +
            "LEFT JOIN class c ON si.cid = c.cid " +
            "WHERE ar.check_type IN ('晚归', '不归') " +
            "<if test='type != null'>" +
            "   AND ar.check_type = #{type} " +
            "</if>" +
            "ORDER BY ar.check_date DESC, ar.check_time DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    @Results({
            @Result(property = "recordId", column = "record_id"),
            @Result(property = "studentId", column = "student_id"),
            @Result(property = "studentName", column = "student_name"),
            @Result(property = "className", column = "class_name"),
            @Result(property = "roomId", column = "room_id"),
            @Result(property = "checkType", column = "check_type"),
            @Result(property = "checkDate", column = "check_date"),
            @Result(property = "checkTime", column = "check_time")
    })
    List<AbnormalStudentDTO> selectAbnormalStudents(Map<String, Object> params);

    @Select("<script>" +
            "SELECT COUNT(ar.record_id) " +
            "FROM attendancerecord ar " +
            "JOIN stu_info si ON ar.student_id = si.student_id " +
            "WHERE ar.check_type IN ('晚归', '不归') " +
            "<if test='type != null'>" +
            "   AND ar.check_type = #{type} " +
            "</if>" +
            "</script>")
    int countAbnormalStudents(Map<String, Object> params);
}
